package com.netx.cubigraf.servlets;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import com.netx.ebs.AuthenticatedServlet;
import com.netx.ebs.EbsRequest;
import com.netx.ebs.EbsResponse;
import com.netx.ebs.Session;
import com.netx.ebs.Permission;
import com.netx.data.DatabaseException;


public class SrvCommonUserInfo extends AuthenticatedServlet {

	public SrvCommonUserInfo() {
		super();
	}

	protected void doGet(EbsRequest request, EbsResponse response) throws IOException, DatabaseException {
		Document document = DocumentHelper.createDocument();
		Element root = document.addElement("netx-application-data");
		root.addAttribute("status", "ok");
		Element user = root.addElement("user");
		Session s = request.getUserSession();
		user.addElement("first-name").setText(CubigrafUtils.getFirstName(s.getUser().getName()));
		Element permElem = user.addElement("permissions");
		Permission[] permissions = s.getUser().getPermissions();
		for(int i=0; i<permissions.length; i++) {
			permElem.addElement("name").setText(permissions[i].getName());
		}

		response.setContentType("text/xml");
		response.sendDisableCache();
		document.write(response.getWriter());
	}

}
